﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using ShootSearch.FileSystem;

namespace ShootSearch.Indexers
{
    public class IndexerAttribute
    {
        public string Name { get; set; }
        public string ColumnName { get; set; }
        public bool AllowNull { get; set; }
        public string Format { get; set; }

        public IndexerAttribute()
        {
            Name = string.Empty;
            AllowNull = true;
            Format = string.Empty;
        }

        public IndexerAttribute(string name)
        {
            this.Name = name;
            ColumnName = name;
            AllowNull = true;
            Format = string.Empty;
        }

        public string GetValue(DateTime value)
        {
            if (value == null) return null;

            if (!string.IsNullOrEmpty(Format))
            {
                try
                {
                    return value.ToString(Format);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                }
            }
            return value.ToString();

        }
        public string GetValue(string value)
        {
            if (!string.IsNullOrEmpty(Format))
            {
                try
                {
                    return string.Format(Format, value);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                }
            }
            return value;
        }

        public string ParseValue(FileObjectText item)
        {
            string value = null;
            switch (Name)
            {
                case FileObjectText.URL:
                    value = GetValue(item.Url);
                    break;
                case FileObjectText.DATETIME:
                    value = GetValue(item.DateTime);
                    break;
                case FileObjectText.ENCODING:
                    value = GetValue(item.Encoding.HeaderName);
                    break;
                default:
                    value = GetValue(item[Name]);
                    break;

            }

            return value;
        }
      
    }
}
